package com.sale.salesystem.mapper;

import com.sale.salesystem.entity.SalesDailyData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Mapper
public interface SalesDailyDataMapper {
    
    // 根据条件查询销售数据
    List<SalesDailyData> selectByConditions(
        @Param("startDate") LocalDate startDate,
        @Param("endDate") LocalDate endDate,
        @Param("category") String category,
        @Param("city") String city,
        @Param("shopId") String shopId,
        @Param("gender") String gender,
        @Param("ageRange") String ageRange
    );

    // 查询指定日期的数据
    List<SalesDailyData> selectByDate(@Param("date") LocalDate date);

    // 查询指定日期范围的数据
    List<SalesDailyData> selectByDateRange(
        @Param("startDate") LocalDate startDate,
        @Param("endDate") LocalDate endDate
    );

    // 获取所有产品类别
    List<String> selectAllCategories();

    // 获取所有城市
    List<String> selectAllCities();

    // 获取所有商店编号
    List<Long> selectAllShopCodes();

    /**
     * 获取最近N天的订单数量数据
     */
    @Select("SELECT DATE_FORMAT(date, '%Y-%m-%d') as date, " +
            "SUM(order_count) as order_count " +
            "FROM sales_daily_data " +
            "WHERE date >= DATE_SUB(CURDATE(), INTERVAL #{days} DAY) " +
            "GROUP BY date " +
            "ORDER BY date")
    List<Map<String, Object>> getRecentOrderCountData(Integer days);

    /**
     * 获取最近N天的销售量数据
     */
    @Select("SELECT DATE_FORMAT(date, '%Y-%m-%d') as date, " +
            "SUM(sales_volume) as sales_volume " +
            "FROM sales_daily_data " +
            "WHERE date >= DATE_SUB(CURDATE(), INTERVAL #{days} DAY) " +
            "GROUP BY date " +
            "ORDER BY date")
    List<Map<String, Object>> getRecentSalesVolumeData(Integer days);

    /**
     * 获取各产品类别的销售数据
     */
    @Select("SELECT category, SUM(total_sales) as total_sales " +
            "FROM sales_daily_data " +
            "GROUP BY category " +
            "ORDER BY total_sales DESC")
    List<Map<String, Object>> getCategorySalesData();
} 